/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.gl;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Currency;
import src.services.gl.CurrencyModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class CurrencyBean implements Serializable {

    private List<Currency> currencys;
    private Currency selectedData, insertedData;
    private boolean editMode = true;
    CurrencyModel helper;       

    public CurrencyBean() {
        this.helper = new CurrencyModel();
        this.currencys = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Currency> getCurrencys() {
        return this.currencys;
    }
    
    public Currency getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Currency selectedData) {
        this.selectedData = selectedData;
    }
    
    public Currency getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Currency insertedData) {
        this.insertedData = insertedData;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Currency();
    }
    
     public void openData() {  
        this.editMode = true;
        this.insertedData = Currency.clone(selectedData);
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.currencys.add(insertedData);
            else {
                int i = currencys.indexOf(selectedData);
                if (i <= currencys.size()) currencys.set(i, Currency.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setCurrencyId(null);
        } 
        
       
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.currencys.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
}
